package com.homestead.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.homestead.bo.CouponThemeConditionBo;
import com.homestead.bo.CouponThemeCreateBo;
import com.homestead.controller.resp.CouponThemeConditionVo;
import com.homestead.domain.dos.CouponThemeCache;
import com.homestead.domain.entity.CouponTheme;
import com.homestead.domain.entity.Page;
import com.homestead.domain.mapper.CouponThemeMapper;
import com.homestead.service.CouponThemeCacheService;
import com.homestead.service.CouponThemeService;
import com.homestead.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author yhb
 * @date 2021-04-09 14:46
 */
@Service
@Slf4j
public class CouponThemeServiceImpl extends ServiceImpl<CouponThemeMapper, CouponTheme> implements CouponThemeService {
    @Autowired
    private CouponThemeCacheService couponThemeCacheService;

    @Override
    public Page<CouponThemeConditionVo> list(CouponThemeConditionBo bo) {
        LambdaQueryWrapper<CouponTheme> qw = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(bo.getThemeTitle())) {
            qw.like(CouponTheme::getThemeTitle, "%" + bo.getThemeTitle() + "%");
        }
        if (Objects.nonNull(bo.getThemeId())) {
            qw.eq(CouponTheme::getId, bo.getThemeId());
        }

        Page<CouponTheme> queryPage = new Page<>();
        queryPage.setCurrent(1L);
        queryPage.setSize(10);
        Page<CouponTheme> pageResult = baseMapper.selectPage(queryPage, qw);

        List<CouponThemeConditionVo> resultList = new ArrayList<>();
        for (CouponTheme po : pageResult.getRecords()) {

            CouponThemeConditionVo vo = new CouponThemeConditionVo();
            BeanUtil.copyProperties(po, vo);
            resultList.add(vo);
        }

        Page<CouponThemeConditionVo> voPage = new Page<>();
        voPage.setCurrent(pageResult.getCurrent());
        voPage.setTotal(pageResult.getTotal());
        voPage.setSize(pageResult.getSize());
        voPage.setRecords(resultList);
        return voPage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean create(CouponThemeCreateBo bo) {
        // 准备couponTheme bean
        CouponTheme couponTheme = prepareCouponThemeBean(bo);
        // db operate
        int result = baseMapper.insert(couponTheme);
        // 创建缓存
        couponThemeCacheService.add(prepareCouponThemeCacheBean(couponTheme));

        return result == 1;
    }

    private CouponThemeCache prepareCouponThemeCacheBean(CouponTheme couponTheme) {
        CouponThemeCache couponThemeCache = new CouponThemeCache();
        BeanUtil.copyProperties(couponTheme, couponThemeCache);
        return couponThemeCache;
    }

    private CouponTheme prepareCouponThemeBean(CouponThemeCreateBo bo) {
        CouponTheme couponTheme = new CouponTheme();
        BeanUtil.copyProperties(bo, couponTheme);
        couponTheme.setId(RedisUtil.generateId());
        couponTheme.setCreateBy("10001");
        couponTheme.setCreateDate(new Date());
        couponTheme.setStatus(4);
        couponTheme.setIsDeleted(0);
        return couponTheme;
    }
}
